package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsAdminMapper;
import com.qf.mapper.DtsBrandMapper;
import com.qf.pojo.DtsAdmin;
import com.qf.pojo.DtsBrand;
import com.qf.service.DtsAdminService;
import com.qf.util.bcrypt.BCryptPasswordEncoder;
import com.qf.vo.CatVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author wanghaotian
 * time:2022/12/16,0016 8:47
 * description:
 */

@Service
public class DtsAdminServiceImpl implements DtsAdminService {

    @Autowired
    private DtsAdminMapper adminMapper;

    @Autowired
    private DtsBrandMapper brandMapper;

    @Override
    public DtsAdmin findAdminByUsername(String username) {
        LambdaQueryWrapper<DtsAdmin> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DtsAdmin::getUsername,username);
        wrapper.eq(DtsAdmin::getDeleted,false);
        DtsAdmin admin = adminMapper.selectOne(wrapper);
        return admin;
    }

    @Override
    public IPage<DtsAdmin> findList(Integer page, Integer limit, String username, String sort, String order) {
        IPage<DtsAdmin> iPage = new Page<>(page,limit);
        QueryWrapper<DtsAdmin> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(username)) {
            wrapper.like("username", username);
        }
        wrapper.eq("deleted",false);
        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            if (order.equalsIgnoreCase("asc")) {
                wrapper.orderByAsc(sort);
            }else {
                wrapper.orderByDesc(sort);
            }
        }
        iPage = adminMapper.selectPage(iPage, wrapper);
        return iPage;
    }

    @Override
    public void addAdmin(DtsAdmin admin) {
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        String password = encoder.encode(admin.getPassword());
        admin.setPassword(password);
        admin.setAddTime(new Date());
        admin.setUpdateTime(new Date());
        admin.setDeleted(false);
        adminMapper.insert(admin);
    }

    @Override
    public void updateAdmin(DtsAdmin admin) {
        DtsAdmin oldAdmin = adminMapper.selectById(admin.getId());
        if (!oldAdmin.getPassword().equals(admin.getPassword())) {
            BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
            String password = encoder.encode(admin.getPassword());
            admin.setPassword(password);
        }
        admin.setUpdateTime(new Date());
        adminMapper.updateById(admin);
    }

    @Override
    public void deleteById(DtsAdmin admin) {
        LambdaQueryWrapper<DtsBrand> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DtsBrand::getAdminId,admin.getId());
        wrapper.eq(DtsBrand::getDeleted,false);
        List<DtsBrand> dtsBrands = brandMapper.selectList(wrapper);
        if (dtsBrands != null) {
            throw new RuntimeException("有品牌绑定，不能删除");
        }
        admin.setDeleted(true);
        adminMapper.updateById(admin);
    }

    @Override
    public List<CatVo> findAll() {
        LambdaQueryWrapper<DtsAdmin> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DtsAdmin::getDeleted,false);
        List<DtsAdmin> adminList = adminMapper.selectList(wrapper);
        List<CatVo> collect = adminList.stream().map((admin) -> {
            CatVo catVo = new CatVo();
            catVo.setValue(admin.getId());
            catVo.setLabel(admin.getUsername());
            return catVo;
        }).collect(Collectors.toList());
        return collect;
    }
}
